<?php
namespace app\zhonghang\controller;
use think\Db;
use think\Controller;
use think\Session;
use traits\model\Transaction;
class Base extends Controller {
 	protected $member;
	protected $mills;
	public function _initialize(){
		parent::_initialize();
// 		header("Content-type: text/html; charset=utf-8");
		header('Access-Control-Allow-Origin:*');
		$token = input( 'token' );
		
		$controller = request()->controller();
		$action = request()->action();
		if($controller.'/'.$action != 'Index/index'){
			if(empty($token)){
				$data['data'] = array();
				$data['status']= -998;
				$data['info']= '请先登录';
				exit(json_encode($data));
			}
			$member = $this->getMemberInfo($token);
			if(empty($member)){
				$data['data'] = array();
				$data['status']= -999;
				$data['info']= '无效token';
				exit(json_encode($data));
			}
		}
		//配置项信息
		$config = Db::name("config")->select();
		foreach ($config as $k=>$v){
			$config[$v['key']]=$v['value'];
		}
		$this->config=$config;
		$this->member=$member;
	}
	/**
	 * 根据token获取用户信息
	 */
	public function getMemberInfo($token) {
//		$member_token = Db::name('App_member_token')->where(array('token'=>$token))->find();
		$member = Db::name('users')->where(array('openid'=>$token))->find();
//		$member = Db::name('App_member_relation')->where(array('member_id'=>$member_token['uid']))->find();
		return $member;
	}
	//二级密码验证
	//         $this->checkTwoPwd();
	//         session('checkTwoPwd_success',null);
	//检测二级密码，显示二级密码页面
	protected function checkTwoPwd(){
		if($_SESSION['checkTwoPwd_success']==null){
			session('url',__ACTION__);
			$this->redirect("Two/twopwd");
		} 
	
	}
    protected  function getMemberByMemberId($id){
    	$where['member_id']=$id;
    	return M('Member')->where($where)->find();
    }
	protected function getUpMemberByZhuceId($jiedianid,$lv,$k=0){
		$user=M("Member")->where("member_id='$jiedianid'")->find();
		$k++;
		if (!empty($user)) {
			$item[]=$user;
			if ($k<$lv) {
				$r=$this->getUpUserByJiedianid($user['zhuceid'],$lv,$k);
				foreach ($r as $v){
					if (!empty($v)) {
						$item[]=$v;
					}
				}
			}
		}
		return $item;
	}
    protected  function getMemberByUsername($email){
    	$where['email']=$email;
    	return M('Member')->where($where)->find();
    }
	//检测是否设置二级密码
	protected function checkTwoPwdSet(){
		if(!$this->member['pwdtrade']){
			//取消二级密码检测  1是通过即不检测 
			session('two_pwd_check',1);
			session('checkTwoPwd_success',rand(111111,999999));
			$this->redirect("SetTradePwd/index");
		}
	}
	//图片处理
	public function upload($file){
		 
		switch($file['type'])
		{
			case 'image/jpeg': $ext = 'jpg'; break;
			case 'image/gif': $ext = 'gif'; break;
			case 'image/png': $ext = 'png'; break;
			case 'image/tiff': $ext = 'tif'; break;
			default: $ext = ''; break;
		}
		if (empty($ext)){
			return false;
		}
		$upload = new \Think\Upload();// 实例化上传类
		$upload->maxSize   =     3145728 ;// 设置附件上传大小
		$upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
		$upload->savePath  =      './Public/Uploads/'; // 设置附件上传目录
		// 上传文件
		$info   =  $upload->uploadOne($file);
		if(!$info) {
			// 上传错误提示错误信息
			$this->error($upload->getError());exit();
		}else{
			// 上传成功
			$pic=$info['savepath'].$info['savename'];
			$url='/Uploads'.ltrim($pic,".");
		}
		return $url;
	}
	/**
	 * 判断会员等级
	 * @param unknown $level
	 */
	public function getUserMoneyByLevel($level){
		$class = Db::name('App_level')->where(array('id'=>$level))->find();
		return $class;
	}
	//查询配置
	public function configs(){
		$list = Db::name('App_config')->select();
		foreach($list as $key=>$value){
			$list[$value['key']] = $value['value'];
		}
		return $list;
	}
	/**
	 * 正则验证
	 * @param unknown $rule
	 * @param unknown $value
	 * @return boolean
	 */
	public function regex($rule,$value) {
		$validate = array(
				'email'     =>  '/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/',
				'url'       =>  '/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(:\d+)?(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/',
				'currency'  =>  '/^\d+(\.\d+)?$/',
				'number'    =>  '/^\d+$/',
				'num'    	=>  '/^[0-9]*$/',
				'zip'       =>  '/^\d{6}$/',
				'integer'   =>  '/^[-\+]?\d+$/',
				'double'    =>  '/^[-\+]?\d+(\.\d+)?$/',
				'english'   =>  '/^[A-Za-z]+$/',
				'idcard'	=>	'/^[1-9]\d{5}[1-9]\d{3}((0\d)|(1[0-2]))(([0|1|2]\d)|3[0-1])\d{3}([0-9]|X)$/i' ,
				// 			'name'		=>  '/[^\D]/g',
				'img'		=>	'/\.(jpg|gif|png)$/i',
				'phone'		=>  '#^13[\d]{9}$|^14[5,7]{1}\d{8}$|^15[^4]{1}\d{8}$|^17[0,6,7,8]{1}\d{8}$|^18[\d]{9}$#',
				'password'  =>  '/^[\w-\.]{6,16}$/',
				'bankcard'  =>  '/^(\d{16}|\d{19})$/',
				'username'  =>  '/^[a-zA-Z][a-zA-Z0-9_]{1,7}$/',
		);
		// 检查是否有内置的正则表达式
		if(isset($validate[strtolower($rule)])){
			$rule       =   $validate[strtolower($rule)];
		}
		return preg_match($rule,$value);
	}
}